package com.bjgraphite.report.controller;

import com.bjgraphite.report.domain.dto.UserInfoDTO;
import com.bjgraphite.report.domain.request.LoginRequest;
import com.bjgraphite.report.enums.BizErrorCode;
import com.bjgraphite.report.exception.LoginExcepiton;
import com.bjgraphite.report.response.CommonResponse;
import com.bjgraphite.report.utils.JWTTokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @PostMapping("/login")
    public CommonResponse<UserInfoDTO> login(@RequestBody LoginRequest loginRequest){
        String password = loginRequest.getPassword();
        String username = loginRequest.getUsername();

        if(StringUtils.isBlank(username) && StringUtils.isBlank(password)){
            throw new LoginExcepiton(BizErrorCode.SYSTEM_INVALID_ARGUMENT);
        }

        if(!"jhtt_dashboard".equals(username) || !"jhtt$%^075!@".equals(password)){
            throw new LoginExcepiton(BizErrorCode.SECURITY_LOGIN_ERROR);
        }

        UserInfoDTO userInfoDTO = new UserInfoDTO();
        String jwtToken = JWTTokenUtils.getJwtToken(Long.valueOf(1000 * 60 * 60 * 24 * 7), JWTTokenUtils.jwtSigner);
        userInfoDTO.setToken(jwtToken);

        return CommonResponse.result(userInfoDTO);

    }
}
